package com.scalar.forecast.columnDisplay.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.columnDisplay.forms.SearchMetaDataForm;
import com.scalar.forecast.columnDisplay.dao.SearchMetaDataDAO;
import com.scalar.chiptrack.db.DBTransaction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Processes the request to save the display details of the search screen
 *
 * Created by Veeru. 
 * Date: Feb 16, 2005
 * Time: 11:08:49 AM 
 */


public class SearchMetaDataSaveCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "editColumnDisplay" );
        SearchMetaDataForm searchMetaDataForm = ( SearchMetaDataForm )form;

        boolean success = false;
        DBTransaction dbTransaction = null;
        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.MESSAGES, errors );

        try {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
            SearchMetaDataDAO.saveMetaData( dbTransaction, searchMetaDataForm.getColumnMetaDataList() );
            //SearchMetaDataDAO.saveMetaData( dbTransaction, searchMetaDataForm.getTableName(), searchMetaDataForm.getColumnMetaDataList() );
            success = true;

        }catch( Exception e ){
        }
        finally {
            if ( success ) {
                try {
                    dbTransaction.commit();
                    errors.put( "Column display details saved successfully", null );
                }catch ( Exception e){
                    e.printStackTrace();
                }
            }else {
                try{
                    dbTransaction.rollback();
                }catch( Exception e ){
                    e.printStackTrace();
                }
                errors.put( "Error : Could not save column display details", null );
            }
        }
        return actionForward;
    }
}
